import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';
import 'package:wanchong/app/home/video.dart';
import 'package:wanchong/conf.dart';
import 'package:wanchong/utils/event_bus.dart';

import 'loading.dart'; 
 
// import 'package:video_player/video_player.dart';
// import 'package:zhmc/public.dart';
class VideoWidget extends StatefulWidget {
  // final VideoInfo info;
  // final String previewImgUrl; //预览图片的地址
  final bool showProgressBar; //是否显示进度条
  final bool showProgressText; //是否显示进度文本
  final int positionTag;
  VideoWidget(
      {Key key,
      // this.previewImgUrl: '',
      this.showProgressBar = true,
      this.showProgressText = true,
      this.positionTag})
      : super(key: key);
  @override
  _VideoWidgetState createState() => _VideoWidgetState();
}

enum VideoState { VideoPlay, VideoPause }

class _VideoWidgetState extends State<VideoWidget> {
  VideoPlayerController _controller;

  bool _hideActionButton = true;
  bool videoPrepared = false; //视频是否初始化
  double width;
  double height;
  bool initialized = false;
  Size size;
  VideoState state;
  @override
  void initState() {
    super.initState();

    eventBus.on(EventVideoPlayPosition + widget.positionTag.toString(), (arg) {
      if (arg == widget.positionTag) {
        _controller.play();
        // size = _controller.value.size;
        // print("sized width=${size.width} height=${size.height} id=${widget.positionTag}");
        videoPrepared = true;
      } else {
        _controller.pause();
      }
      setState(() {});
    });
    _controller = VideoPlayerController.network("widget.info.url")
      ..initialize()
      ..setLooping(true).then((_) {
        initialized = true;
        if (widget.positionTag == 0) {
//          VideoDetailScene.firstInitTimes = 2;

          _controller.play();
          videoPrepared = true;
        }
        setState(() {});
      });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        GestureDetector(
          child: Stack(
            children: <Widget>[
              _controller.value.initialized
                  ? Container(
                      color: Colors.black,
                      child: Center(
                        child: AspectRatio(
                          aspectRatio: _controller.value.aspectRatio,
                          child: VideoPlayer(_controller),
                        ),
                      ),
                    )
                  : Container(
                      color: Colors.black,
                    ),
              pauseView(),
            ],
            // child: Container(
            //     color: Colors.white,
            //     width: 200,
            //     height: 50,
            //     child: Container(
            //       height: 100.0,
            //       width: 100.0,
            //       child: Container(color: Colors.red,)
            //     ),
            //   ),
          ),
          onDoubleTap: () {
            if (_controller.value.isPlaying) {
              _controller.pause();
              _hideActionButton = false;
            } else {
              _controller.play();
              videoPrepared = true;
              _hideActionButton = true;
            }
            setState(() {});
          },
        ),
        Container(
          child: Column(
            children: <Widget>[
              SizedBox(
                height: ScreenUtil.getInstance().setHeight(60.0),
              ),
              Container(
                // color: Colors.red,
                width: double.infinity,
                height: ScreenUtil.getInstance().setHeight(50.0),
                child: Row(
                  // mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      width: ScreenUtil.getInstance().setWidth(15.0),
                    ),
                    ClipOval(
                      child: CachedNetworkImage(
                        imageUrl: "widget.info.header",
                      ),
                    ),
                    SizedBox(
                      width: 10.0,
                    ),
                    Text(
                      "widget.info.nick",
                      style: TextStyle(
                        fontSize: ScreenUtil.getInstance().setSp(18.0),
                        fontWeight: FontWeight.w700,
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: Container(),
              ),
              Container(
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Column(
                        children: <Widget>[
                          Container(
                            child: Text("widget.info.desc"),
                          ),
                          Container(
                            margin: EdgeInsets.only(
                              top: ScreenUtil.getInstance().setHeight(29.0),
                              bottom: ScreenUtil.getInstance().setHeight(35.0),
                            ),
                            child: Row(
                              children: <Widget>[
                                SizedBox(
                                  width:
                                      ScreenUtil.getInstance().setWidth(15.0),
                                ),
                                Text(
                                  "widget.info.red.toString()",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize:
                                        ScreenUtil.getInstance().setSp(12.0),
                                  ),
                                ),
                                SizedBox(
                                  width: ScreenUtil.getInstance().setWidth(8.0),
                                ),
                                Text(
                                  "阅读量",
                                  style: TextStyle(
                                    color: const Color(0xFFB4BEC6),
                                    fontSize:
                                        ScreenUtil.getInstance().setSp(12.0),
                                  ),
                                ),
                                SizedBox(
                                  width:
                                      ScreenUtil.getInstance().setWidth(35.0),
                                ),
                                Text(
                                  "widget.info.agree.toString()",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize:
                                        ScreenUtil.getInstance().setSp(12.0),
                                  ),
                                ),
                                SizedBox(
                                  width: ScreenUtil.getInstance().setWidth(8.0),
                                ),
                                Text(
                                  "个赞",
                                  style: TextStyle(
                                    color: const Color(0xFFB4BEC6),
                                    fontSize:
                                        ScreenUtil.getInstance().setSp(12.0),
                                  ),
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      width: ScreenUtil.getInstance().setWidth(30.0),
                      margin: EdgeInsets.only(
                        left: ScreenUtil.getInstance().setWidth(27.0),
                        right: ScreenUtil.getInstance().setWidth(15.0),
                      ),
                      child: Column(
                        children: <Widget>[
                          GestureDetector(
                            onTap: () {
                              //关注
                            },
                            child: Container(child: Icon(Icons.local_movies),),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        )
      ],
    );
  }

  @override
  void dispose() {
    eventBus.off(EventVideoPlayPosition + widget.positionTag.toString());
    _controller.dispose(); //释放播放器资源
    super.dispose();
    // eventBus.off("aaa" + widget.positionTag.toString());
  }

  pauseView() {
    return Offstage(
      offstage: _hideActionButton,
      child: Stack(
        children: <Widget>[
          Align(
            child: Container(
                child: Image.asset('assets/ic_playing.png'),
                height: 50,
                width: 50),
            alignment: Alignment.center,
          )
        ],
      ),
    );
  }
}
